<%@ include file="/WEB-INF/views/includes/include.jsp" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Choose your location</title>
<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q">
</script>
<style type="text/css" media="screen">
body {
margin:0;
padding:0;
height:100%; /* this is the key! */

}

#mapsLocation {
min-height:400px; /*ff*/
height: auto !important; /*ie7 */
height: 400px; /*ie6 */
padding:0;
width:95%;
color:#333;
background:#eaeaea;
border:1px solid #333;
} 

#controlPanel{
padding:0;
width:95%;
height:15%; /* works only if parent container is assigned a height value */
color:#444;
background:#eaeaea;
border:1px solid #333;
} 
</style>
</head>


<body>

<div id="mapsLocation">
<img src="images/loading1.gif"></img>
</div>
<form:form method="post" action="location.html" name="locationform" onsubmit="return checkvalidity()">



<div id="controlPanel" >
Control Panel
<p align="right">
<input type="hidden" name="cCode" value="${cd}">
<input type="hidden" name="lat" value="37.4419">
<input type="hidden" name="lon" value="-122.1419">

<input type="Submit" value="Next">
</p>
</div>
</form:form>

<script language="javascript">
var isValid=true;
var homeLat=37.4419;
var homeLon=-122.1419;
var country="${cd}";
var map;
var homeMarker;
var geocoder;
var latlng=new GLatLng(homeLat,homeLon );

function checkvalidity()
{
if(!isValid)
{
alert("Place your home on a valid location in your country " + country+ " before continuing");
return false;
}
else
return true;
}

function initMap()
{
geocoder = new GClientGeocoder();
geocoder.setBaseCountryCode(country);
geocoder.getLatLng(country,showMap);
}

 function showMap(latlng) {
  map = new GMap2(document.getElementById("mapsLocation"));
  map.setCenter(latlng, 5);
  map.addControl(new GSmallMapControl());
  homeMarker = new GMarker(latlng, {draggable: false}); 
 map.addOverlay(homeMarker); 
 document.locationform["lon"].value=latlng.lng();
 document.locationform["lat"].value=latlng.lat();;
    
	  GEvent.addListener(map, "click", function(overlay,latlng) {
if(latlng==null) return;
		    map.removeOverlay(homeMarker); 
                     homeMarker = new GMarker(latlng, {draggable: false});  
 					map.addOverlay(homeMarker); 
		 			 geocoder.getLocations(latlng, showAddress);
		});
      
    }

function showAddress(response) {

  if (!response || response.Status.code != 200) {
   alert("This is a disputed location.You cannot setup your base in this area!");
   isValid=false;
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
    document.locationform["lon"].value=place.Point.coordinates[0];
    document.locationform["lat"].value=place.Point.coordinates[1];
    
 // homeMarker.openInfoWindowHtml(   'Setting new home at ' + place.address + '</br>' + ' in country ' + place.AddressDetails.Country.CountryNameCode);
 homeMarker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' + 
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);


if(place.AddressDetails.Country.CountryNameCode!=country)
{
isValid=false;
alert("You can setup your base only in your home country");
}
else
{
isValid=true;
}

  }
}

initMap();
</script>

</body>
</html>
